package com.woniu.woniuproject.controller.admin;

import com.github.pagehelper.PageInfo;
import com.woniu.woniuproject.entity.Account;
import com.woniu.woniuproject.entity.Funding_not_matched;
import com.woniu.woniuproject.entity.Product_account;
import com.woniu.woniuproject.mapper.AccountMapper;
import com.woniu.woniuproject.service.FundQueueService;
import com.woniu.woniuproject.service.ProductAccountService;
import com.woniu.woniuproject.utils.ResponseResult;
import com.woniu.woniuproject.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 功能描述:<br>
 * 〈〉
 *
 * @author EDZ
 * @create 2020/11/18
 * @since 1.0.0
 */
@RestController
@RequestMapping("FundQueue")
public class FundQueueController {

    @Autowired
    FundQueueService fundQueueService;

    @Autowired
    ProductAccountService productAccountService;

    @Autowired
    AccountMapper accountMapper;
    @GetMapping("queryall")
    public ResponseResult<PageInfo<Funding_not_matched>>  queryall(Page page,Funding_not_matched funding_not_matched){
        PageInfo<Funding_not_matched> list  = fundQueueService.queryall(page,funding_not_matched);
        return new ResponseResult<>(list);
    }

    @GetMapping("queryall2")
    public ResponseResult<PageInfo<Funding_not_matched>>  queryall2(Page page,Funding_not_matched funding_not_matched){
        PageInfo<Funding_not_matched> list  = fundQueueService.queryall2(page,funding_not_matched);
        return new ResponseResult<>(list);
    }

    @GetMapping("unupdateone")
    public ResponseResult<Integer>  unupdateone(Funding_not_matched funding_not_matched){
        int i=fundQueueService.updateStatus1(funding_not_matched);
        return new ResponseResult<>(i);
    }


    //待匹配资金列表通过
    @GetMapping("updateone")
    public ResponseResult<Integer>  updateone(@RequestParam("start") String start, Funding_not_matched funding_not_matched){
        Integer uid = funding_not_matched.getFinvestrecordid();
        Double fnotmatchedmoney = funding_not_matched.getFnotmatchedmoney();//带匹配金额
        Account account=accountMapper.selectByKey(uid);
        Double balance = account.getBalance();//可用余额

        if(balance-fnotmatchedmoney<0){
            fundQueueService.updateStatus1(funding_not_matched);
            return new ResponseResult<>(-1);
        }

        Date startDates = null;
        try {
            if (start!=null&&start!=""){
                startDates = new SimpleDateFormat( "yyyy-MM-dd").parse(start);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        funding_not_matched.setFcreatedate(startDates);
        //更新用户交易表
        int i=Integer.parseInt(funding_not_matched.getFfoundingtype());//产品类型
        Product_account product_account = new Product_account();
        product_account.setPproductid(Integer.parseInt(funding_not_matched.getFfoundingtype()));
        product_account.setPuid(funding_not_matched.getFinvestrecordid());
        Date date=new Date();
        product_account.setPmatchdate(date);
        if(i==1){
            date.setMonth(date.getMonth()+1);
        }else if(i==2){
            date.setMonth(date.getMonth()+2);
        }else if(i==3){
            date.setMonth(date.getMonth()+3);
        }
        product_account.setPenddate(date);
        product_account.setPredeemdate(date);
        product_account.setPamount(funding_not_matched.getFnotmatchedmoney());
        double m=funding_not_matched.getFnotmatchedmoney();//投资金额
        product_account.setPproducttype("1");
        product_account.setPexpectedannualincome(funding_not_matched.getPexpectedannualincome());
        double v=funding_not_matched.getPexpectedannualincome();//年利率
        if(i==1){
            product_account.setPmonthinterest(v/1200*m);
        }else if(i==2){
            product_account.setPmonthinterest(v/1200*m);
        }else if(i==3){
            product_account.setPmonthinterest(v/1200*m);
        }
        product_account.setPearningsisfinished("0");
        if(i==1){
            product_account.setPendinvesttotalmoney(v/1200*m+m);
        }else if(i==2){
            product_account.setPendinvesttotalmoney(v/400*m+m);
        }else if(i==3){
            product_account.setPendinvesttotalmoney(v/200*m+m);
        }
        List<Product_account> list=productAccountService.queryByUid(funding_not_matched.getFinvestrecordid());
        double money=funding_not_matched.getFnotmatchedmoney();
        for(int k=0;k<list.size();k++){
            money+=list.get(k).getPamount();
        }
        product_account.setPtotal(money);
        String  no=fundQueueService.updateStatus(funding_not_matched);
        product_account.setPremark(no);//插入合同号
        int success=productAccountService.insert(product_account);
        return new ResponseResult<>(success);
    }


}